﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medical.Business;
using System.IO;
using Medical.Entity;
using System.Data;

/// <summary>
/// 广告操作页面
/// </summary>
public partial class webadmin_DoBanner : AdminPage
{
    /// <summary>
    /// ID
    /// </summary>
    protected int nID;

    /// <summary>
    /// 广告类型
    /// </summary>
    protected AdverTypeEnum eType;

    /// <summary>
    /// 操作类型
    /// </summary>
    protected string strType = string.Empty;

    /// <summary>
    /// 图片大小
    /// </summary>
    protected string strImgSize = string.Empty;

    protected string strImgPix = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        strType = Common.GetQueryValue("type", 0);
        eType = (AdverTypeEnum)Convert.ToInt32(Common.GetQueryValue("advertype", 0));
        strImgSize = WebConfigInfo.FileSize < 1024 * 1024 ? (WebConfigInfo.FileSize / 1024 + "K") : (WebConfigInfo.FileSize / (1024 * 1024) + "M");
        if (!Page.IsPostBack)
        {
            switch (eType)
            {
                case AdverTypeEnum.HomeAdv:
                    strImgPix = "382px*283px";
                    break;
                case AdverTypeEnum.Honor:
                    strImgPix = "135px*98px";
                    LinkDiv.Style["display"] = "none";
                    break;
                case AdverTypeEnum.Subject:
                    strImgPix = "274px*94px";
                    break;
                case AdverTypeEnum.Environment:
                    strImgPix = "179px*127px";
                    LinkDiv.Style["display"] = "none";
                    break;
                case AdverTypeEnum.Banner:
                    strImgPix = "980px*175px";
                    NameDiv.Style["display"] = "none";
                    LinkDiv.Style["display"] = "none";
                    break;
                case AdverTypeEnum.FontDisease:
                    ImgDiv1.Visible = false;
                    ImgDiv2.Visible = false;
                    SortDiv.Visible = true;
                    InitSort();
                    break;
                case AdverTypeEnum.Health:
                case AdverTypeEnum.Health1:
                    ImgDiv1.Visible = false;
                    ImgDiv2.Visible = false;
                    SortDiv.Visible = false;
                    break;
                case AdverTypeEnum.Disease:
                    strImgPix = "283px*382px";
                    SortDiv.Visible = true;
                    InitSort();
                    break;
            }
        }
        if (strType == "edit")
        {
            //获取ID
            nID = Convert.ToInt32(Common.GetQueryValue("id", 0));
            if (!Page.IsPostBack)
            {
                //页面数据初始化
                InitData();
            }
        }
    }

    protected void InitSort()
    {
        BisSortInfo bisSortInfo = new BisSortInfo();
        //绑定分类下拉框
        DataTable dtSortList = bisSortInfo.GetSortList(0,ArticelTypeEnum.Disease);
        if (dtSortList != null && dtSortList.Rows.Count > 0)
        {
            int nSortIndex = 0;
            foreach (DataRow dr in dtSortList.Rows)
            {
                ddlSort.Items.Add(new ListItem(Common.GetSortIcon(nSortIndex) + dr["Name"].ToString(), dr["ID"].ToString()));
            }
            dtSortList.Dispose();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.Adver;
    }

    /// <summary>
    /// 页面数据初始化
    /// </summary>
    protected void InitData()
    {
        Banner Info = new BisBanner().Get(nID);
        if (Info != null)
        {
            txtName.Text = Info.Name;
            hfImg.Value = Info.ImgPlace;
            txtLink.Text = Info.Url;
            ImgDiv.Visible = true;
            if (!string.IsNullOrEmpty(Info.ImgPlace))
            {
                imgAdver.ImageUrl = "~/" + WebConfigInfo.FilePlace + "/d/" + Info.ImgPlace;
            }
            else
            {
                imgAdver.Visible = false;
            }
        }      
    }


    /// <summary>
    /// 提交操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        bool result = false;
        BisBanner bisBanner = new BisBanner();
        Banner Info = new Banner();
        Info.Name = txtName.Text.Trim();
        Info.BannerType = (int)eType;
        Info.Url = txtLink.Text.Trim();
        if (ddlSort.SelectedValue != string.Empty)
            Info.SubBannerType = Convert.ToInt32(ddlSort.SelectedValue);
        string strImg = string.Empty;

        //上传图片
        if (fuImg.HasFile)
        {
            result = UpImg(fuImg, ref strImg);
            if (!result)
                return;
        }
        else if (strType == "add" && eType != AdverTypeEnum.FontDisease && eType != AdverTypeEnum.Health && eType != AdverTypeEnum.Health1)
        {
            Until.Alert("请选择要上传的图片！");
            return;
        }

        if (strType == "add")
        {
            //新增信息
            Info.ImgPlace = strImg;
            result = bisBanner.Insert(Info);
        }
        else if (strType == "edit")
        {
            Info.ImgPlace = (strImg == string.Empty ? hfImg.Value : strImg);
            Info.ID = nID;
            result = bisBanner.Update(Info);
            if(result&&!string.IsNullOrEmpty(strImg))
                Until.DeleteFile(Server.MapPath("~/" + WebConfigInfo.FilePlace + "d/" + hfImg.Value));
        }
        //结果处理
        if (result)
        {
            Until.AlertJS("提交成功！", "ResetPage();");
        }
        else
        {
            Until.Alert("提交失败！");
        }
        Until.WebBanner = null;
    }

    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="fileImg"></param>
    /// <param name="strImg"></param>
    /// <returns></returns>
    protected bool UpImg(FileUpload fileImg, ref string strImg)
    {
        string strExtension = Path.GetExtension(fileImg.FileName).ToLower();
        if (fileImg.PostedFile.ContentLength > WebConfigInfo.FileSize)
        {
            Until.Alert("图片大小不超过" + strImgSize + "！");
            return false;
        }
        //验证图片格式
        if (Until.CheckFileType(0, strExtension))
        {
            string strFileName = Guid.NewGuid().ToString().Replace("-", "") + strExtension;
            string strYear = DateTime.Now.Year.ToString();
            string strMonth = DateTime.Now.Month.ToString();

            //原图
            string strFile = Server.MapPath("~/" + WebConfigInfo.FilePlace + "d/"+strYear + "/"+strMonth+"/");
            if (!Directory.Exists(strFile))
            {
                Directory.CreateDirectory(strFile);
            }
            fileImg.SaveAs(strFile + strFileName);
            strImg = strYear + "/" + strMonth+"/"+strFileName;
            return true;
        }
        else
        {
            Until.Alert("图片格式不正确！");
            return false;
        }
    }
}